package edu.ptu.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.ptu.acl.common.MyConstants;
import edu.ptu.acl.dto.UserTransferDto;
import edu.ptu.acl.dto.param.UserParam;
import edu.ptu.acl.entity.Dept;
import edu.ptu.acl.entity.User;
import edu.ptu.acl.exception.ApiException;
import edu.ptu.acl.mapper.DeptMapper;
import edu.ptu.acl.mapper.UserMapper;
import edu.ptu.acl.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.ptu.acl.utils.MD5Util;
import edu.ptu.acl.utils.RedisUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LinJC
 * @since 2022-03-15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private DeptMapper deptMapper;

    @Override
    public void updateStatus(UserParam userParam) {
        User user = User.builder().id(userParam.getId()).status(userParam.getStatus())
                .version(userParam.getVersion()).build();

        if (userMapper.updateById(user)==0) {
            throw new ApiException(MyConstants.ResultCode.OPTIMISTIC_LOCKING);
        }

        // 如果用户在线，强制下线
        if (RedisUtil.getValue(user.getId().toString())!=null) {
            RedisUtil.setValue(user.getId().toString(),"",0);
        }

    }

    @Override
    public void updatePassword(Long id, String password) {
//        password = MD5Util.encrypt(password);
        User user = User.builder().id(id).password(password).build();
        if (userMapper.updateById(user)==0) {
            throw new ApiException(MyConstants.ResultCode.OPTIMISTIC_LOCKING);
        }
        // 如果用户在线，强制下线
        if (RedisUtil.getValue(id.toString())!=null) {
            RedisUtil.setValue(user.getId().toString(),"",0);
        }
    }

    @Override
    public Page<User> getPageUserByCondition(User user,Page page) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (user!=null){
            if (StringUtils.isNotBlank(user.getName())){
                queryWrapper.like("name",user.getName());
            }
            if (StringUtils.isNotBlank(user.getTelephone())){
                queryWrapper.like("telephone",user.getTelephone());
            }
            if (StringUtils.isNotBlank(user.getStatus())){
                queryWrapper.eq("status",user.getStatus());
            }
            if (StringUtils.isNotBlank(user.getEmail())){
                queryWrapper.like("email",user.getEmail());
            }
        }
        userMapper.selectPage(page,queryWrapper);

        return page;
    }

    @Override
    public void recursionGetDeptUser(Long deptId, List<UserTransferDto> userTransferDtoList) {
        QueryWrapper<Dept> deptQueryWrapper = new QueryWrapper<>();
        deptQueryWrapper.eq("parent_id",deptId);
        List<Dept> deptList = deptMapper.selectList(deptQueryWrapper);
        if (CollectionUtils.isEmpty(deptList)){
            return;
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        for (Dept dept : deptList) {
            queryWrapper.clear();
            queryWrapper.eq("dept_id",dept.getId());
            List<User> users = userMapper.selectList(queryWrapper);
            if (CollectionUtils.isNotEmpty(users)){
                for (User user : users) {
                    userTransferDtoList.add(UserTransferDto.copyUser2Dto(user));
                }
            }
            recursionGetDeptUser(dept.getId(),userTransferDtoList);
        }
    }
}
